﻿using AutoMapper;

namespace Tszy.Unlimited.Base.Infrastructure.Mappers;

/// <summary>
/// 
/// </summary>
public class TszyMapper
{
    private readonly IMapper _dictionaryMapper = new MapperConfiguration(cfg => { }).CreateMapper();
    private readonly IDictionary<string, IMapper> _objectMapper = new Dictionary<string, IMapper>();

    /// <summary>
    /// 实例。
    /// </summary>
    public static TszyMapper Instance { get; private set; } = new TszyMapper();

    private TszyMapper()
    {

    }

    /// <summary>
    /// 
    /// </summary>
    public IMapper GetDictionaryMapper() => _dictionaryMapper;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sourceType"></param>
    /// <param name="destinationType"></param>
    /// <returns></returns>
    public IMapper GetObjectMapper(Type sourceType, Type destinationType)
    {
        var key = $"{sourceType.FullName}_{destinationType.FullName}";

        if (!_objectMapper.ContainsKey(key))
        {
            var config = new MapperConfiguration(cfg => cfg.CreateMap(sourceType, destinationType));
            var mapper = config.CreateMapper();

            _objectMapper[key] = mapper;
        }

        return _objectMapper[key];
    }
}